#include "Report.h"

namespace {
const QString IGridppReportGuid = "{F9364159-6AED-4f9c-8BAF-D7C7ED6160A8}";

} // namespace

Report::Report(QWidget* parent)
    : report_(new QAxObject(parent))
    , detailGrid_(nullptr)
    , recordSet_(nullptr)
    , columnContent_(nullptr)
{
    report_->setControl(IGridppReportGuid);
}

QAxObject* Report::GetReport() const
{
    return report_;
}

void Report::LoadFromFile(const QString& reportFile)
{
    report_->dynamicCall("LoadFromFile(string)", reportFile);
}

void Report::PrintPreview(bool showModal)
{
    report_->dynamicCall("PrintPreview(bool)", showModal);
}

void Report::Print(bool showModal)
{
    report_->dynamicCall("Print(bool)", showModal);
}

void Report::ClearColumns()
{
    DetailGrid()->dynamicCall("ClearColumns()");
}

QAxObject* Report::AddColumn(
    const QString& Name,
    const QString& Title,
    const QString& DataField,
    double Width
)
{
    QAxObject* column = DetailGrid()->querySubObject(
        "AddColumn(string,string,string,double)", Name, Title, DataField, Width
    );
    QAxObject* textFormat =
        column->querySubObject("TitleCell")->querySubObject("TextFormat");
    textFormat->setProperty("TextAlign", grtaMiddleCenter);
    return column;
}

QAxObject* Report::AddControl(
    QAxObject* column,
    ReportControlType controlType,
    const QString& Name,
    double Left,
    double Top,
    double Width,
    double Height,
    const QString& DataField
)
{
    QAxObject* cell = column->querySubObject("ContentCell()");
    QAxObject* control = cell->querySubObject("Controls()")
                             ->querySubObject("Add(int)", controlType);
    control->setProperty("Name", Name);
    control->setProperty("Height", Height);
    control->setProperty("Width", Width);
    control->setProperty("Left", Left);
    control->setProperty("Top", Top);
    control->setProperty("ShrinkFontToFit", true);
    if (controlType == grctFieldBox) {
        control->querySubObject("AsFieldBox()")
            ->setProperty("DataField", DataField);
    }

    return control;
}

void Report::setFontPoint(QAxObject* control, double point)
{
    control->querySubObject("Font")->setProperty("Point", point);
}

QAxObject* Report::AddField(const QString& Name, ReportFieldType fieldType)
{
    return RecordSet()->querySubObject("AddField(string,int)", Name, fieldType);
}

void Report::ColumnFreeCell(QAxObject* column, bool free)
{
    QAxObject* cell = column->querySubObject("ContentCell()");
    cell->setProperty("FreeCell", free);
}

void Report::setPrarameter(const QString& name, const QString& value)
{
    QAxObject* field;
    field = report_->querySubObject("ParameterByName(string)", name);
    field->setProperty("AsString", value);
}

void Report::setField(const QString& name, const QString& value)
{
    QAxObject* field;
    field = report_->querySubObject("FieldByName(string)", name);
    field->setProperty("AsString", value);
}

void Report::setControlText(const QString& name, const QString& text)
{
    try {
        QAxObject* control =
            report_->querySubObject("ControlByName(string)", name);
        if (!control) {
            qDebug() << "Control not found: " << name;
            return;
        }
        QAxObject* staticBox = control->querySubObject("AsStaticBox()");
        staticBox->setProperty("Text", text);
    } catch (std::exception& e) {
        qDebug() << e.what();
    }
}

void Report::setControlImage(const QString& name, const QString& imagePath)
{
    try {
        QAxObject* control =
            report_->querySubObject("ControlByName(string)", name);
        if (!control) {
            qDebug() << "Control not found: " << name;
            return;
        }
        QAxObject* pictureBox = control->querySubObject("AsPictureBox()");
        pictureBox->dynamicCall("LoadFromFile(string)", imagePath);
    } catch (std::exception& e) {
        qDebug() << e.what();
    }
}

void Report::Append()
{

    RecordSet()->dynamicCall("Append()");
}

void Report::Post()
{
    RecordSet()->dynamicCall("Post()");
}

QAxObject* Report::DetailGrid()
{
    if (!detailGrid_) {
        detailGrid_ = report_->querySubObject("DetailGrid");
    }
    return detailGrid_;
}

QAxObject* Report::RecordSet()
{
    if (!recordSet_) {
        recordSet_ = DetailGrid()->querySubObject("RecordSet");
    }
    return recordSet_;
}

QAxObject* Report::ColumnContent()
{
    if (!columnContent_) {
        columnContent_ = DetailGrid()->querySubObject("ColumnContent");
    }
    return columnContent_;
}
